package com.jinlejia.erp.ui.mine.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.ui.mine.transfer.TransferApplyMoreListActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author SongJiaTai
 * @description:异动三级联动第一列
 * @date :2021/3/2 15:18
 */
public class TransFerOneListAdapter extends YfListAdapter<Map<String, Object>> {
    public int cur_position = -1;
    private Map<Integer, Boolean> checkMap = new HashMap<>();
    private Context context;

    public TransFerOneListAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    public TransFerOneListAdapter(Context context, ArrayList<Map<String, Object>> data) {
        super(data);
        this.context = context;
    }

    @Override
    public int getItemCount() {
        for (int i = 0; i < mData.size(); i++) {
            if (cur_position == i) {
                checkMap.put(i, true);
            } else {
                checkMap.put(i, false);
            }
        }
        return super.getItemCount();
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_selector_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
        ((ViewHolder) viewHolder).selectName.setText(mData.get(i).get("name").toString());
        if ( ((ArrayList<Map<String, Object>>)mData.get(i).get("list")).size()>0){
            if (checkMap.get(i)) {
                ((ViewHolder) viewHolder).selectName.setTextColor(context.getResources().getColor(R.color.yellow));
                ((ViewHolder) viewHolder).selectName.setChecked(true);
                ((ViewHolder) viewHolder).select_icon.setVisibility(View.GONE);
                ((ViewHolder) viewHolder).et_left.setVisibility(View.GONE);
                ((ViewHolder) viewHolder).layout.setBackgroundResource(R.drawable.dark_gray_bg);
            } else {
                ((ViewHolder) viewHolder).selectName.setTextColor(context.getResources().getColor(R.color.black));
                ((ViewHolder) viewHolder).selectName.setChecked(false);
                ((ViewHolder) viewHolder).select_icon.setVisibility(View.GONE);
                ((ViewHolder) viewHolder).et_left.setVisibility(View.GONE);
                ((ViewHolder) viewHolder).layout.setBackgroundResource(R.color.white);
            }
        }else{
            if (checkMap.get(i)) {
                ((ViewHolder) viewHolder).selectName.setTextColor(context.getResources().getColor(R.color.yellow));
                ((ViewHolder) viewHolder).selectName.setChecked(true);
                ((ViewHolder) viewHolder).select_icon.setVisibility(View.VISIBLE);
                ((ViewHolder) viewHolder).layout.setBackgroundResource(R.drawable.dark_gray_bg);
                if (mData.get(i).get("name").equals("其他") || mData.get(i).get("name").equals("校园招聘")){
                    ((ViewHolder) viewHolder).et_left.setVisibility(View.VISIBLE);
                    ((ViewHolder) viewHolder).et_left.addTextChangedListener(new TextSwitcher());
                }
            } else {
                ((ViewHolder) viewHolder).selectName.setTextColor(context.getResources().getColor(R.color.black));
                ((ViewHolder) viewHolder).selectName.setChecked(false);
                ((ViewHolder) viewHolder).select_icon.setVisibility(View.GONE);
                ((ViewHolder) viewHolder).et_left.setVisibility(View.GONE);
                ((ViewHolder) viewHolder).layout.setBackgroundResource(R.color.white);
            }
        }

        viewHolder.itemView.setTag(mData.get(i));
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (cur_position != -1) {
                    if (cur_position!=i){
                        checkMap.put(cur_position, false);
                        notifyItemChanged(cur_position);
                        onItemRemoveListener.onItemRemove(mData.get(cur_position));
                    }
                }
                if (cur_position != i) {
                    cur_position = i;
                    checkMap.put(i, true);
                    notifyItemChanged(i);
                    onItemSelectedListener.onItemSelected(mData.get(i));
                }
            }
        });
    }

    private static final class ViewHolder extends RecyclerView.ViewHolder {
        //选项名
        CheckBox selectName;
        //选择图标
        ImageView select_icon;
        //布局
        RelativeLayout layout;
        //其他输入框
        EditText et_left;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            selectName = itemView.findViewById(R.id.id_city_name);
            select_icon = (ImageView) itemView.findViewById(R.id.selector_icon);
            layout = (RelativeLayout) itemView.findViewById(R.id.layout);
            et_left = itemView.findViewById(R.id.et_left);
        }
    }

    /**
     * 选中
     */

    public interface OnItemSelectedListener {
        void onItemSelected(Map<String, Object> map);
    }

    private OnItemSelectedListener onItemSelectedListener;

    public void setOnItemSelectedListener(OnItemSelectedListener listener) {
        this.onItemSelectedListener = listener;
    }

    /**
     * 移除
     */
    public interface OnItemRemoveListener {
        void onItemRemove(Map<String, Object> map);
    }

    private OnItemRemoveListener onItemRemoveListener;

    public void setOnItemRemoveListener(OnItemRemoveListener listener) {
        this.onItemRemoveListener = listener;
    }

    //EditText 监听
    class TextSwitcher implements TextWatcher{

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            ((TransferApplyMoreListActivity)context).saveEditStringData(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
